package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.DbManager;

import java.util.ArrayList;
import java.util.List;

import zj.com.db.DBUtil;
import zj.com.model.Dic;
import zj.com.model.JcxDB;
import zj.com.model.UserInfo;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.Constants;
import zj.com.util.DialogUtil;
import zj.com.util.SharedPreferencesUtil;
import zj.com.zj.R;

/**
 * Created by xi on 16/5/31.
 */
public class LoginActivity extends  BaseActivity implements View.OnClickListener{

    public ImageView left_iv;
    public TextView title_name;
    public TextView right_tv;

    public EditText username_et;
    public EditText pwd_et;
    public TextView login_tv;

    public Intent intent;
    public ProgressDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView(){

       left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setVisibility(View.VISIBLE);
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setVisibility(View.GONE);
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("用户登录");

        username_et = (EditText) findViewById(R.id.username_et);
        pwd_et = (EditText) findViewById(R.id.pwd_et);
        login_tv = (TextView) findViewById(R.id.login_tv);
        login_tv.setOnClickListener(this);

        Object obj = SharedPreferencesUtil.getData(this, "username", "");
        if(obj != null){
            username_et.setText(obj.toString());

        }

       // username_et.setText("proMgr_egs_azsgc");


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_iv:

                finish();
                break;
            case R.id.login_tv:

                doLogin();
                break;

        }
    }

    public void doLogin(){

        String name = username_et.getText().toString();
        String pwd = pwd_et.getText().toString();
        if(TextUtils.isEmpty(name)){

            Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(pwd)){

            Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        dialog = DialogUtil.createProgressDialog(this,"提示","登录中...");//创建ProgressDialog

        NetManager.getInstance(this).doLogin(name, pwd, new OnResponseListener<UserInfo>() {
            @Override
            public void onComplete(boolean isOK, UserInfo result, int errorCode, String errorMsg) {

                if(isOK){
                    dialog.setMessage("初始化数据中...");
                    obtainPrivilege();

                }else{
                    dialog.dismiss();
                    Toast.makeText(LoginActivity.this,errorMsg,Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                dialog.dismiss();
                Toast.makeText(LoginActivity.this,errorMessage,Toast.LENGTH_SHORT).show();
            }
        });
    }

    public  void obtainPrivilege(){

        NetManager.getInstance(this).GetPrivilege(new OnResponseListener<List<JcxDB>>() {
            @Override
            public void onComplete(boolean isOK, List<JcxDB> result, int errorCode, String errorMsg) {
                sync();
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                dialog.dismiss();
            }
        });
    }

    public int totalCount = 0;
    public int count = 0;
    public void sync(){

        DbManager dbManager = DBUtil.getInstance(this).getDBManager();
        try{
            dbManager.delete(Dic.class);
            List<String> list = new ArrayList<String>();
            list.add(Constants.InspectType);
            list.add(Constants.UserLevel);
            list.add(Constants.AttachType);
            list.add(Constants.InspectionState);
            list.add(Constants.RiskType);
            list.add(Constants.RiskLevel);
            list.add(Constants.RepairRecordState);
            list.add(Constants.ProjectState);
            list.add(Constants.OrgLevel);
            list.add(Constants.AuditState);
            totalCount = list.size();

            for(String str:list){
                NetManager.getInstance(this).obtainDic(str, new OnResponseListener<List<Dic>>() {
                    @Override
                    public void onComplete(boolean isOK, List<Dic> result, int errorCode, String errorMsg) {


                        count ++;

                        if (isOK) {

                            if(count == totalCount){
                                dialog.dismiss();
                                intent = new Intent(LoginActivity.this, MainTabActivity.class);
                                startActivity(intent);
                                finish();
                            }

                        } else {
                            Toast.makeText(LoginActivity.this, "失败", Toast.LENGTH_SHORT).show();

                        }

                    }

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        dialog.dismiss();

                        Toast.makeText(LoginActivity.this, "失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }

        }catch(Exception e){
            dialog.dismiss();
            Toast.makeText(LoginActivity.this, "失败", Toast.LENGTH_SHORT).show();

        }

    }
}
